package com.wmeimob.fastboot.jiubei.service;

import com.wmeimob.fastboot.core.exception.CustomException;
import com.wmeimob.fastboot.jiubei.dto.ShopCartDTO;
import com.wmeimob.fastboot.jiubei.entity.ShopCart;
import com.wmeimob.fastboot.jiubei.mapper.ShopCartMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @ClassName ShopCartServiceImpl
 * @Description 购物车 服务类实现
 * @Author wjt
 * @Date Thu Aug 20 10:06:35 CST 2020
 **/

@Service
@Transactional(rollbackFor = {Exception.class})
@Slf4j
public class ShopCartServiceImpl implements ShopCartService {

    @Resource
    private ShopCartMapper shopCartMapper;

    private void assertUserInfo(ShopCart shopCart) {

        if (shopCart.getUserId() == null) {
            throw new IllegalArgumentException("user info is missing");
        }
    }

    @Override
    public List<ShopCartDTO> queryShopCartList(ShopCart shopCart) {
        assertUserInfo(shopCart);
        return shopCartMapper.selectSkuList(shopCart);
    }

    @Override
    public Integer queryCount(ShopCart shopCart) {
        Example example = new Example(ShopCart.class);
        example.createCriteria().andEqualTo("userId", shopCart.getUserId())
                .andEqualTo("del", false);
        return shopCartMapper.selectCountByExample(example);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ShopCart addShopCart(ShopCart shopCart) {
        assertUserInfo(shopCart);
        int selectCount = checkSkuCount(shopCart);
        if (selectCount < 1) {
            shopCart.setGmtCreate(new Date());
            shopCart.setInvalid(false);
            shopCartMapper.insertSelective(shopCart);
            return shopCart;
        }
        shopCart.setGmtModified(new Date());
        int result = shopCartMapper.incrementCount(shopCart);
        if (result < 1) {
            throw new CustomException("添加购物车失败");
        }
        return shopCart;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteShopCart(ShopCart shopCart) {
        Integer[] idArr = shopCart.getIdArr();
        if (idArr == null || idArr.length == 0) {
            throw new CustomException("没有删除任何选项");
        }
        Example example = new Example(ShopCart.class);
        example.createCriteria().andIn("id", Arrays.asList(idArr)).andEqualTo("userId", shopCart.getUserId());
        ShopCart updateShopCart = new ShopCart();
        updateShopCart.setGmtModified(new Date());
        updateShopCart.setDel(true);
        int result = shopCartMapper.updateByExampleSelective(updateShopCart, example);
        if (result == 0) {
            throw new CustomException("没有删除任何选项");
        }
        return result;
    }

    @Override
    public void orderAfterClear(Integer userId, List<Integer> goodsIdList, List<String> skuNoIds) {
        ShopCart shopCart = new ShopCart();
        shopCart.setDel(true);
        Example example = new Example(ShopCart.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("userId", userId).andEqualTo
                ("del", false).andIn("goodsId", goodsIdList);
        if (skuNoIds != null && !skuNoIds.isEmpty()) {
            criteria.andIn("skuNo", skuNoIds);
        }
        shopCartMapper.updateByExampleSelective(shopCart, example);
    }

    private int checkSkuCount(ShopCart shopCart) {
        ShopCart checkCountDTO = new ShopCart();
        checkCountDTO.setGoodsId(shopCart.getGoodsId());
        checkCountDTO.setSkuNo(shopCart.getSkuNo());
        checkCountDTO.setUserId(shopCart.getUserId());
        checkCountDTO.setDel(false);
        return shopCartMapper.selectCount(checkCountDTO);
    }
}
